<?php

namespace app\rent\controller;

use think\Controller;

class Category extends Controller
{
    protected $CategoryModel = null;

    public function __construct()
    {
        parent::__construct();

        // 加载模型
        $this->CategoryModel = model('Category');
    }

    public function index()
    {
        $page = $this->request->param('page', 1, 'trim');

        $limit = 5;

        $start = ($page - 1) * $limit;

        $list = $this->CategoryModel->order('id DESC')->limit($start, $limit)->select();

        if ($list) {
            $this->success('查询成功', null, $list);
        } else {
            $this->error('暂无数据', null, []);
        }
    }

    public function info()
    {
        $id = $this->request->param('id', 0, 'trim');
        $busid = $this->request->param('busid', 0, 'trim');

        $info = $this->CategoryModel->find($id);

        if (!$info) {
            $this->error('文章不存在');
        }

        $where = [
            'cateid' => $id,
            'busid' => $busid
        ];

        $Collection = model('Business.Collection')->where($where)->find();

        $Collection = $Collection ? true : false;

        // 上一篇
        $prev = $this->CategoryModel->where('id', '<', $id)->order('id DESC')->find();

        // 下一篇
        $next = $this->CategoryModel->where('id', '>', $id)->order('id ASC')->find();

        // 上一篇文章 下一篇文章 当前文章
        $data = [
            'info' => $info,
            'Collection' => $Collection,
            'prev' => $prev,
            'next' => $next
        ];

        $this->success('查询成功', null, $data);
    }

    // 轮播图
    public function swiper()
    {
        $HotData = $this->CategoryModel->where(['flag' => ['like', "%hot%"]])->order('createtime DESC')->limit(6)->select();

        $this->success('查询成功', null, $HotData);
    }

    // 收藏文章
    public function collection()
    {
        $busid = $this->request->param('busid', 0, 'trim');
        $cateid = $this->request->param('cateid', 0, 'trim');

        // $category = $this->CategoryModel->find($cateid);

        // if (!$category) {
        //     $this->error('文章不存在');
        // }

        // $Business = model('Business.Business')->find($busid);

        // if (!$Business) {
        //     $this->error('非法登录', null, ['action' => 'login']);
        // }

        // 组装条件
        $where = [
            'cateid' => $cateid,
            'busid' => $busid
        ];

        $collection = model('Business.Collection')->where($where)->find();

        // 提示信息
        $msg = '';

        $result = null;

        $status = false;

        if ($collection) {
            // 在收藏表能找到数据，说明已经收藏过 => 取消收藏
            $result = $collection->delete();

            $msg = '取消收藏';
        } else {
            // 说明没有收藏过 => 收藏
            $data = [
                'cateid' => $cateid,
                'busid' => $busid,
            ];

            $result = model('Business.Collection')->validate('common/Business/Collection')->data($data)->save();
            // exit(model('Business.Collection')->getLastSql());
            $msg = '收藏';

            $status = true;
        }

        if ($result === FALSE) {
            $this->error($msg . '失败');
        } else {
            $this->success($msg . '成功', null, ['status' => $status]);
        }
    }
}
